package com.kaishengit.action;

import java.text.ParseException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.interceptor.ServletRequestAware;

import com.kaishengit.dao.core.Page;
import com.kaishengit.pojo.Deal;
import com.kaishengit.pojo.Note;
import com.kaishengit.pojo.Task;
import com.kaishengit.pojo.User;
import com.kaishengit.service.DealService;
import com.kaishengit.service.NoteService;
import com.kaishengit.service.TaskService;
import com.opensymphony.xwork2.ActionSupport;

public class DealAction extends ActionSupport implements ServletRequestAware {

	private static final long serialVersionUID = 1L;
	private DealService dealService;
	private Deal deal;
	private String contactid;
	private HttpServletRequest request;
	private Page<Deal> page = new Page<Deal>();
	private int pageNum;
	private String progress;
	private String id;
	private Note note;
	private List<Note> listNote;
	private NoteService noteService;
	private String dealid;
	private TaskService taskService;
	private List<Task> dealTaskList;
	/**
	 * 去交易的页面 我在这里默认显示的是搜有的跟踪的交易
	 */
	public String execute(){
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("curr_user");
		page.setPageNum(pageNum);
		if(progress==null){
			page = dealService.findAllPendingDeal(user,"pend",page);
			return SUCCESS;
		}else{
			page = dealService.findAllPendingDeal(user,progress,page);
			List<Deal> dealList = page.getResult();
			for(Deal deal:dealList){
				deal.setUser(null);
				deal.setNoteSet(null);
				deal.setDcSet(null);
			}
			return "json";
		}
	}
	/**
	 * 编辑交易
	 */
	public String editDeal(){
		deal = dealService.findDealById(id);
		return "success";
		
		
	}
	public String update(){
		System.out.println(deal.getId());
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("curr_user");
		System.out.println(deal.getContactname());
		dealService.updateDeal(user,deal,contactid);
		return SUCCESS;
	}
	/**
	 * 去添加交易页面new_deal.jsp
	 * @param dealService
	 */
	public String add(){
		return SUCCESS;
	}
	/**
	 * 添加交易
	 * @return
	 */
	public String save(){
		System.out.println(contactid);
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("curr_user");
		System.out.println(deal.getContactname());
		dealService.save(user,deal,contactid);
		return SUCCESS;
	}
	/**
	 * 到单个交易的页面
	 * @return
	 * @throws ParseException 
	 */
	public String tosingledeal() throws ParseException{
		deal = dealService.findDealById(id);

		//查找所有的交易记录
		listNote = noteService.findNoteByDealId(id); 

		//listNote = noteService.findNoteByDealId(id); 

		//查找与该交易相关的日程
		dealTaskList = taskService.findTaskByType(deal.getId());
		return "success";
		
	}
	/**
	 * 更新状态  pend won lost
	 * @return
	 */
	public String updatestate(){
		note = dealService.update(id,progress);
		return SUCCESS;
	}
	/**
	 * 删除交易
	 * @param dealService
	 */
	public String delDeal(){
		dealService.delDeal(id);
		return SUCCESS;
	}
	
	//set get
	public void setDealService(DealService dealService) {
		this.dealService = dealService;
	}
	public Deal getDeal() {
		return deal;
	}
	public void setDeal(Deal deal) {
		this.deal = deal;
	}
	public String getContactid() {
		return contactid;
	}
	public void setContactid(String contactid) {
		this.contactid = contactid;
	}
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
		
	}
	public Page<Deal> getPage() {
		return page;
	}
	public void setPage(Page<Deal> page) {
		this.page = page;
	}
	public int getPageNum() {
		return pageNum;
	}
	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}
	public String getProgress() {
		return progress;
	}
	public void setProgress(String progress) {
		this.progress = progress;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public Note getNote() {
		return note;
	}
	public void setNote(Note note) {
		this.note = note;
	}
	public List<Note> getListNote() {
		return listNote;
	}
	public void setListNote(List<Note> listNote) {
		this.listNote = listNote;
	}
	public void setNoteService(NoteService noteService) {
		this.noteService = noteService;
	}
	public void setTaskService(TaskService taskService) {
		this.taskService = taskService;
	}
	public List<Task> getDealTaskList() {
		return dealTaskList;
	}
	public void setDealTaskList(List<Task> dealTaskList) {
		this.dealTaskList = dealTaskList;
	}
	
	
}
